package com.qidianit.mes.masterdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidianit.mes.common.pojo.MesResult;
import com.qidianit.mes.masterdata.mapper.UserMapper;
import com.qidianit.mes.masterdata.mapper.WorkshopMapper;
import com.qidianit.mes.masterdata.service.WorkshopService;
import com.qidianit.mes.pojo.Workshop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2025/8/1 10:14
 * @description
 */
@Service
@Transactional
public class WorkShopServiceImpl implements WorkshopService {
    @Autowired
    private WorkshopMapper workshopMapper;
    @Autowired
    private UserMapper userMapper;// userService;
    @Override
    public MesResult<Page<Workshop>> getWorkshopList(Workshop workshop, int pageNum, int pageSize) {
        Page<Workshop> page = new Page<>(pageNum, pageSize);
        return MesResult.success(workshopMapper.selectWorkshopList(page, workshop));
    }

    @Override
    public MesResult<Workshop> saveWorkshop(Workshop workshop) {
        return workshopMapper.insert(workshop)==1?MesResult.success():MesResult.error();
    }

    @Override
    public MesResult<Workshop> getWorkshopById(Integer id) {
        Workshop workshop = workshopMapper.selectById(id);
        if (workshop != null) {
            workshop.setManager(userMapper.selectById(workshop.getManagerId()));
        }
        return MesResult.success(workshop);
    }

    @Override
    public MesResult<Workshop> deleteWorkshop(Integer id) {
        Workshop workshop = workshopMapper.selectById(id);
        workshop.setDeletedFlag(1);
        return workshopMapper.updateById(workshop) == 1?MesResult.success():MesResult.error();
    }

    @Override
    public MesResult<Workshop> updateWorkshop(Workshop workshop) {
        return workshopMapper.updateById(workshop) == 1?MesResult.success():MesResult.error();
    }

    @Override
    public MesResult deleteWorkshopByIds(Integer[] ids) {
        // 使用MyBatisPlus的方式处理
        UpdateWrapper<Workshop> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids); // 设置where体检
        wrapper.set("deleted_flag", 1);// 设置要修改的字段及值
        return MesResult.success(workshopMapper.update(null, wrapper));
    }

//    @Override
//    public MesResult<Page<Workshop>> getWorkshopList(Workshop workshop, int pageNum, int pageSize) {
//        Page<Workshop> page = new Page<>(pageNum, pageSize);
//        // 准备一个查询的wrapper
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq(workshop != null && workshop.getWorkshopCode() != null && !workshop.getWorkshopCode().isEmpty(), "workshop_code", workshop.getWorkshopCode());
//        wrapper.like(workshop != null && workshop.getWorkshopName() != null && !workshop.getWorkshopName().isEmpty(), "workshop_name", workshop.getWorkshopName());
//        return MesResult.success(workshopMapper.selectPage(page, wrapper));
//    }
}
